<?php
namespace Admin\Controller;

class OrganizationController extends AbstractController  {
	
	protected $From;
	
	public function _initialize(){
		$this->Form = D('Department');
    }
	  
    public function index(){
		$this->assign('list',$this->show(0));
        $this->display();
    }
	
	public function save($type = false, $id = 0){
		if($type != false){
			if($type == "add"){
				if(!$this->Form->create()){
					exit($this->error($this->Form->getError()));
				}else{
					$result = $this->Form->add();
					if($result){
						$this->success('添加成功！');
					}else{
						$this->error('添加错误！');
					}
				}
			}else if($type == "update"){
				$this->Form->_validate = array(
					array('Name','require','请输入部门名称'),
					array('ParentID','require','父ID不能为空')
				);
				if(!$this->Form->create()){  // 父ID验证不过，目的是要跳转编辑页面
					$sql = "SELECT a.*,(SELECT Name FROM department WHERE ID = a.ParentID AND IsDel = 0) AS PName, IF ((SELECT ID FROM department WHERE ID = a.ParentID AND IsDel = 0), (SELECT ID FROM department WHERE ID = a.ParentID AND IsDel = 0) , 0) AS Pid, b.UserName AS BossName, b.ID AS BossID FROM department AS a LEFT JOIN user AS b ON a.BossID = b.ID WHERE a.ID = $id AND a.IsDel = 0";
					$res = $this->Form->query($sql);
					$this->assign('PName',$res[0]['PName']);
					$this->assign('data',$res[0]);
					$this->assign('update',true);
					
					$Authrule = D("department")->where(array('ID'=>$id))->find();
			        $result = D("department")->order(array("ID" => "desc"))->select();
			        $tree = new \Think\Tree();
			        foreach ($result as $r) {
			            $r['selected'] = $r['ID'] == $Authrule['ParentID'] ? 'selected' : '';
			            $array[] = $r;
			        }
			        $str = "<option value='\$ID' \$selected>\$spacer \$Name</option>";
			        $tree->init($array);
			        $select_categorys = $tree->get_tree(0, $str);
			        $this->assign("select_categorys", $select_categorys);
	        
					$this->display();
				}else{
					$result = $this->Form->save();
					if($result) {
						$this->success('修改成功！');
					}else{
						$this->error('修改错误！');
					}
				}
			}else if($type == "delete"){
				$show = $this->show($id);
				if(empty($show)){
					$sql = " UPDATE department SET IsDel = 1 WHERE ID = {$id}";
					$res = $this->Form->execute($sql);
					if($res){
						$this->success('部门删除！');
					}else{
						$this->error('部门删除！');
					}
				}else{
					$this->error('请先删除子部门！');
				}		
			}
		}else{
			if($id){
				$sql = "SELECT * FROM department WHERE ID = $id";
				$res = $this->Form->query($sql);
				$this->assign('PName',$res[0]['Name']);
			}
			$this->display();
		}
	}
	
	/**
	 返回部门列表数据
	 */
	public function show($pid = -1){
		if($pid == -1){
			$in = I();
			$parentID = $in['ParentID'];
		}else{
			$parentID = $pid;
		}
		$sql = "SELECT *,(SELECT count(id) FROM department WHERE ParentID = a.ID AND IsDel = 0) AS coutsonID FROM department AS a WHERE ParentID = {$parentID} AND IsDel = 0  ORDER BY OrderNO ASC";
		$res = $this->Form->query($sql);
		if($pid == -1){
			$this->ajaxReturn($res);
		}else{
			return $res;
		}
    }
    
    
    /**
     * 返回选人窗口部门树数据
     */
    public function selectorTree(){
    	//$jsonid = json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
		$jsonid = json_decode(file_get_contents("php://input"));
    	$id = $jsonid->id ? $jsonid->id : 0;
    	$return = $this->Form->getSelectorTree((int)$id);
    	$this->ajaxReturn($return);
    }
    
	
}